package com.kj.platformlogistics.systemnotification;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kj.platformlogistics.R;
import com.kj.platformlogistics.base.BaseActivity;
import com.kj.platformlogistics.base.BaseModel;
import com.kj.platformlogistics.constants.UrlConstants;
import com.kj.platformlogistics.home.activity.SolicilationedDetailActivity;
import com.kj.platformlogistics.home.adapter.SoliciLationedAdapter;
import com.kj.platformlogistics.home.model.SolicilationedModel;
import com.kj.platformlogistics.http.HttpRequest;
import com.kj.platformlogistics.http.HttpStringCallBack;
import com.kj.platformlogistics.systemnotification.adapter.NotificationAdapter;
import com.kj.platformlogistics.util.JsonUtils;
import com.kj.platformlogistics.util.WarningDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.kj.platformlogistics.constants.Constants.PAGESIZE;

public class SystemNotification extends BaseActivity implements View.OnClickListener {

    private TextView _barTvRight;
    private RecyclerView rcCommon;
    private SmartRefreshLayout refresh;
    private LinearLayout llNodata;
    private List<SystemNotifiModel.ResultDTO> data;
    private NotificationAdapter adapter;
    private LinearLayout llBottom;
    private ImageView ivSelectAll;
    private TextView tvDelete;
    private List<String> idList=new ArrayList<>();

    private boolean isDelete=false;
    private Boolean isSelectall=false;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, SystemNotification.class);
        context.startActivity(intent);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id._barTvRight:
                //编辑s
                if (!isDelete){
                    llBottom.setVisibility(View.VISIBLE);
                    for (int i=0;i<data.size();i++){
                        data.get(i).setShow(true);
                    }
                    adapter.notifyItemRangeChanged(0,data.size()-1,R.id.ivSelected);
                    _barTvRight.setText("完成");
                }else {
                    llBottom.setVisibility(View.GONE);
                    _barTvRight.setText("编辑");
                    for (int i=0;i<data.size();i++){
                        data.get(i).setShow(false);
                    }
                    adapter.notifyItemRangeChanged(0,data.size()-1,R.id.ivSelected);
                }
                isDelete=!isDelete;
                break;
            case R.id.tvDelete:
                //删除
                WarningDialog delete=new WarningDialog(this,
                        "清除已经选中的历史记录吗？",
                        "取消","确定","");
               delete.setClicklistener(new WarningDialog.ClickListenerInterface() {
                   @Override
                   public void doCancel() {
                       delete.dismiss();
                   }

                   @Override
                   public void doConfirm() {
                        delete.dismiss();
                        for (int i=0;i<data.size();i++){
                            if (data.get(i).getSelected()){
                                idList.add(data.get(i).getId());
                            }
                        }
                        delData();
                   }
               });
                delete.show();
                break;
            case R.id.ivSelectAll:
                if (!isSelectall){
                    for (int i=0;i<data.size();i++){
                        data.get(i).setSelected(true);
                    }
                    tvDelete.setBackgroundColor(getResources().getColor(R.color.red));
                    ivSelectAll.setImageResource(R.mipmap.icon_address_selected);
                }else {
                    for (int i=0;i<data.size();i++){
                        data.get(i).setSelected(false);
                    }
                    tvDelete.setBackgroundColor(getResources().getColor(R.color.gray9));
                    ivSelectAll.setImageResource(R.mipmap.icon_address_unselected);
                }
                adapter.notifyDataSetChanged();
                isSelectall=!isSelectall;
                break;
        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_notification;
    }

    @Override
    protected void initView() {
        setTitleText("系统通知");
        _barTvRight=findViewById(R.id._barTvRight);
        refresh=findViewById(R.id.refresh);
        _barTvRight.setTextColor(getResources().getColor(R.color.red));
        setRightText("编辑");
        _barTvRight.setText("编辑");
        ivSelectAll=findViewById(R.id.ivSelectAll);
        llBottom=findViewById(R.id.llBottom);
        tvDelete=findViewById(R.id.tvDelete);
        tvDelete.setOnClickListener(this::onClick);
        _barTvRight.setOnClickListener(this::onClick);
        ivSelectAll.setOnClickListener(this::onClick);
        rcCommon=findViewById(R.id.rcCommon);
        llNodata=findViewById(R.id.llNodata);
        getData();

        if (data!=null&&data.size()>0){
            _barTvRight.setVisibility(View.VISIBLE);
        }else {
            _barTvRight.setVisibility(View.GONE);
        }
    }

    //获取数据
    public void getData(){

        new HttpRequest(SystemNotification.this).doPost(UrlConstants.logistics_getLogisticsMessage, null, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        refresh.finishRefresh();
                        Object obj = JsonUtils.fromJson(result.toString(), SystemNotifiModel.class);
                        if (obj instanceof SystemNotifiModel) {
                            SystemNotifiModel model=(SystemNotifiModel) obj;
                                if (model.getResult().size() > 0) {
                                    _barTvRight.setVisibility(View.VISIBLE);
                                    llNodata.setVisibility(View.GONE);
                                    rcCommon.setVisibility(View.VISIBLE);
                                    data=model.getResult();
                                    adapter=new NotificationAdapter(data);
                                    rcCommon.setLayoutManager(new LinearLayoutManager(SystemNotification.this));
                                    rcCommon.setAdapter(adapter);
                                    adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                        @Override
                                        public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                            switch (view.getId()){
                                                case R.id.ivSelected:
                                                    if (data.get(position).getSelected()){
                                                        data.get(position).setSelected(false);
                                                    }else {
                                                        data.get(position).setSelected(true);
                                                    }
                                                    checkUtil();
                                                    adapter.notifyDataSetChanged();
                                                    break;
                                            }
                                        }
                                    });
                                } else {
                                    _barTvRight.setVisibility(View.GONE);
                                    rcCommon.setVisibility(View.GONE);
                                    refresh.finishRefreshWithNoMoreData();
                                    llNodata.setVisibility(View.VISIBLE);
                                }
                            }
                    }else {
                        Toast.makeText(SystemNotification.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                refresh.finishLoadMore();
                refresh.finishRefresh();
            }
        });
    }

    public void checkUtil(){
        for (int i=0;i<data.size();i++){
            if (!data.get(i).getSelected()){
               ivSelectAll.setImageResource(R.mipmap.icon_address_unselected);
            }else {
                ivSelectAll.setImageResource(R.mipmap.icon_address_selected);
            }
        }

        for (int i=0;i<data.size();i++){
            if (data.get(i).getSelected()){
                tvDelete.setBackgroundColor(getResources().getColor(R.color.red));
            }else {
                tvDelete.setBackgroundColor(getResources().getColor(R.color.gray9));
            }
        }
    }

    @Override
    protected void initData() { }

    /**
     * 获取要使用的标题栏样式(此函数由子类重写)
     * @return
     */
    @Override
    protected int getTitleBarType() {
        return 0;
    }



    //获取数据
    public void delData(){
        JSONObject jsonObject=new JSONObject();
        JSONArray jsonArray=new JSONArray(idList);
        try {
            jsonObject.put("idList",jsonArray);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new HttpRequest(SystemNotification.this).doPost(UrlConstants.logistics_deleteLogisticsMessage, null,jsonObject, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        getData();
                        llBottom.setVisibility(View.GONE);
                    }else {
                        Toast.makeText(SystemNotification.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                refresh.finishLoadMore();
                refresh.finishRefresh();
            }
        });
    }
}
